package com.smartcruise.util;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

/**
 * Created by FengChaoQun
 * on 2018/2/27 0027
 */

public class TimeUtils {

    public static String getDateTimeString(long milliseconds, String format) {
        Date date = new Date(milliseconds);
        SimpleDateFormat formatter = new SimpleDateFormat(format, Locale.getDefault());
        return formatter.format(date);
    }

    public static String getSimpleTimeString(long milliseconds) {
        return getDateTimeString(milliseconds, "yyyy-MM-dd HH:mm");
    }

    public static long currentTime() {
        return System.currentTimeMillis();
    }

    public static String getUsedTime(long usedTime) {
        long hour = usedTime / (60 * 60 * 1000);
        long minute = usedTime % (60 * 60 * 1000) / (60 * 1000);
        return hour + "小时" + minute + "分钟";
    }

    /**
     * description:获取制定一天的开始时间
     */

    public static Date getDayBeginTime(Calendar calendar) {
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        return new Date(calendar.getTimeInMillis());
    }

    /**
     * description:获取制定一天的结束时间
     */

    public static Date getDayEndTime(Calendar calendar) {
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        return new Date(calendar.getTimeInMillis());
    }

    public static boolean isSameDay(Date date1, Date date2) {
        Calendar cal1 = Calendar.getInstance();
        Calendar cal2 = Calendar.getInstance();
        cal1.setTime(date1);
        cal2.setTime(date2);

        boolean sameDay = cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR) &&
                cal1.get(Calendar.DAY_OF_YEAR) == cal2.get(Calendar.DAY_OF_YEAR);
        return sameDay;
    }

}
